<?php

    $rootDir = $_SERVER['DOCUMENT_ROOT'];

    require_once($rootDir.'/classes/db/SQLiteConnector.class.php');   
    require_once($rootDir.'/classes/db/LeproUserDAO.class.php');
    require_once($rootDir.'/classes/SessionManager.class.php');    
    require_once($rootDir.'/classes/LeproHelper.class.php');

    $userId = urldecode($_GET['userId']);
    $dirtyMode = false;
    if ($userId[0] == '.') {
        $iuserId = substr($userId, 1);
        $dirtyMode = true;
    } else $iuserId = $userId;
    if (is_numeric($iuserId)) {
        settype($iuserId, 'integer'); 
        $userId = $dirtyMode ? ($iuserId + LeproHelper::$DIRTY_OFFSET) : $iuserId;          
        $connector = SQLiteConnector::getInstance();
        if ($connector->connect()) {            
            $userDAO = new LeproUserDAO($connector->getDB());
            $user = $userDAO->load($userId);
            $loginCode = $dirtyMode ? null : LeproHelper::getLoginCode();            
            $loggedIn = ($loginCode == null);
            SessionManager::clearSessionData();  
            SessionManager::prepareUser($userId);           
            if ($user != null) {
                echo json_encode(array(
                    'userid' => $user->getID(),
                    'username' => $user->getUsername(),
                    'email' => $user->getEmail(),
                    'srvlogged' => $loggedIn,
                    'logincode' => $loginCode
                ));
            } else {
                echo json_encode(array(
                    'userid' => '?'.$userId,
                    'srvlogged' => $loggedIn,
                    'logincode' => $loginCode
                ));
            }
        } else {
            echo '-1';
        }
    } else {
        echo '-2';
    }

?>
